package com.maduixa.guitar.tabsearch.utils;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class EncryptionUtils {

	private static SecretKeySpec key=null;

	public static void initCipher() throws Exception{
		String passphrase = "Maduixa inc passphrase";
		MessageDigest digest;
		digest = MessageDigest.getInstance("SHA");
		digest.update(passphrase.getBytes());
		key = new SecretKeySpec(digest.digest(), 0, 16, "AES");
	}
	
	public static byte[] encrypt(String s) throws Exception{
		if (key == null) initCipher();
		Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
		aes.init(Cipher.ENCRYPT_MODE, key);
		byte[] ciphertext = aes.doFinal(s.getBytes());
		return ciphertext;
	}
	
	public static String decrypt(byte[] s) throws Exception{
		if (key == null) initCipher();
		Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
		aes.init(Cipher.DECRYPT_MODE, key);
		String cleartext = new String(aes.doFinal(s));
		return cleartext;
	}
}